﻿'  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
'  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
'  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
'  PURPOSE.
'
'  Author:  Mojo/All YMSG 16 decryption credit goes to Martin Johnson of www.ExplicitStudios.com 
'  in essence this is a vb.net version  of his vb6 release... 


Imports System.Security.Cryptography
Imports System.Text

Module ymCrypt
    Private Const Operands As String = "[+|&%^]"

    Public Function FormatChallengeOperands(ByVal Challenge As String) As String
        Dim bChallenge As String = Nothing

        For Count As Integer = Challenge.Length To 1 Step -1
            If Mid(Challenge, Count, 1) Like Operands Then
                bChallenge = "%" & Hex(Asc(Mid$(Challenge, Count, 1))).ToLower
            Else
                bChallenge = Mid(Challenge, Count, 1)
            End If
        Next

        FormatChallengeOperands = bChallenge
        Return FormatChallengeOperands
    End Function

    Public Function YahooAuthHash(ByVal Crumb As String, ByVal Challenge As String) As String
        YahooAuthHash = MD5(Crumb & Challenge)
        YahooAuthHash = MAC64(YahooAuthHash)

    End Function

    Private Function MD5(ByVal inputStr As String) As String

        Dim strTmp As String
        Dim strTmp2 As String = Nothing
        Dim sTb As Byte() = UnicodeEncoding.Default.GetBytes(inputStr)
        Dim scB As Byte() = CType(CryptoConfig.CreateFromName("MD5"),  _
        HashAlgorithm).ComputeHash(sTb)
        Dim h1 As String = BitConverter.ToString(scB).Replace("-", Nothing)
        For i As Integer = 1 To h1.Length Step 2
            strTmp = Chr(CInt(Val("&H" & h1.Substring(i - 1, 2))))
            strTmp2 = strTmp2 + strTmp
        Next i
        MD5 = strTmp2
        Return MD5
    End Function

    Private Function MAC64(ByVal inputStr As String) As String
        Dim inBytes As Byte() = System.Text.Encoding.Default.GetBytes(inputStr)
        MAC64 = Convert.ToBase64String(inBytes). _
        Replace("+", ".").Replace("/", "_").Replace("=", "-")
        Return MAC64
    End Function

End Module
